// pages/goods/edit/index.js
const app = getApp()
import http from '../../../utils/request.js';
import alert from '../../../utils/alert.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    table:{},
    goodsList:[],
    orderNo:[],
    editGoods:{},
    mealsData:[],
    scrollHeight:'500px',
    mealGoodsList:[],
    popupTitle:"套餐名字",
    mealsPopup:false,
    attrPopup:false,
    dinnerStatus:0,
    pinzhu:'',
    startDinner:{
      0:"正常",
      1:"加急",
      2:"等叫"
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var storageData=wx.getStorageSync('storageOrder');
    
    var title = storageData.table.name+': 已点菜品';
    wx.setNavigationBarTitle({
      title:title
    })
    var res = wx.getSystemInfoSync();
    //遍历goods_list
    this.setData({
      table: storageData.table,
      goodsList: storageData.goods_list,
      orderNo: storageData.order_no,
      totalPrice: storageData.totalPrice,
      scrollHeight: res.windowHeight - 81 + 'px'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showToast({
      title: '点击菜品设置备注和上餐顺序',
      icon:'none',
      duration: 3000
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  setGoods:function(e){
    console.log(e)
    var goods=e.currentTarget.dataset.goods;
    var mealGoodsList = this.data.mealGoodsList;

    if (typeof (goods.is_start_dinner) == 'undefined'){
      goods.is_start_dinner=0;
    }
    console.log(goods.is_start_dinner);
    if (typeof (goods.pinzhu) == 'undefined') {
      goods.pinzhu = '';
    }

    if (goods.goods_id > 0 && goods.meal_id > 0) {
      mealGoodsList['mg' + goods.meal_id + '-' + goods.goods_id] = goods;

      this.setData({
        attrPopup: true,
        editGoods: goods,
        dinnerStatus: goods.is_start_dinner,
        pinzhu: goods.pinzhu,
        mealGoodsList: mealGoodsList
      })
    }else{
      this.setData({
        attrPopup: true,
        editGoods: goods,
        dinnerStatus: goods.is_start_dinner,
        pinzhu: goods.pinzhu
      })
    }
  },
  onChange(e) {
    if (e.detail.key.indexOf(this.key) !== -1) {
      return wx.showModal({
        title: 'No switching is allowed',
        showCancel: !1,
      })
    }

    this.setData({
      current: e.detail.key,
    })
  },

  onMeals:function(e){
    this.setData({
      mealsPopup: false
    })
  },

  pinzhuChange:function(e){
    this.setData({
      pinzhu: e.detail.value
    })
  },

  dinnerChange:function(e){
    this.setData({
      dinnerStatus:parseInt(e.detail.value)
    })
  },

  queryPopup:function(e){
    //遍历更新商品
    var goods = this.data.editGoods;
    var goodsList = this.data.goodsList;
    var pinzhu=this.data.pinzhu;
    var is_start_dinner = this.data.dinnerStatus
    for (var i = 0; i < goodsList.length; i++){
      if (goodsList[i].meal_id > 0) {
        for (var j = 0; j < goodsList[i].goods.length; j++){
          if (goodsList[i].goods[j].goods_id == goods.goods_id && goodsList[i].goods[j].meal_id == goods.meal_id) {
            goodsList[i].goods[j].pinzhu = pinzhu
            goodsList[i].goods[j].is_start_dinner = is_start_dinner
            break;
          }
        }
      } else {
        if (goodsList[i].goods_id == goods.goods_id && goodsList[i].sku_id == goods.sku_id) {
          goodsList[i].pinzhu = pinzhu
          goodsList[i].is_start_dinner = is_start_dinner
          break;
        }
      }
    }
    this.setData({
      attrPopup: false,
      goodsList: goodsList
    })
  },

  onCloseAttrPopup:function(e){
    this.setData({
      attrPopup:false
    })
  },
  orderGoods:function(e){
    //把所有套餐里边商品提取出来
    var orderGoods=[];
    var goodsList = this.data.goodsList;
    for (var i = 0; i < goodsList.length; i++) {
      orderGoods.push(goodsList[i])
      if (goodsList[i].meal_id > 0) {
        for (var j = 0; j < goodsList[i].goods.length; j++) {
          orderGoods.push(goodsList[i].goods[j])
        }
      }
    }
    var params = {
      order_info:{
        table_id:this.data.table.id,
        order_no:this.data.orderNo,
        person_num:this.data.table.person_num,
        remark:"",
        is_start_dinner:0
      },
      goods_list: orderGoods
    };
    http.post(app.config.service.submitGoods, params).then(data => {
      wx.setStorageSync('storageOrder',{})
      wx.redirectTo({
        url: '../../order/info/index?order_no='+data.order_no,
      })

    }, err => {
      console.log(err)
      alert.alert(err)
    });
  }
  
})